<?php
declare(strict_types=1);

namespace app\admin\controller;

use app\admin\service\PackService;
use app\AdminController;

class Pack extends AdminController
{
    /**
     * 社群礼包列表
     * @return \think\response\Json|\think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        if (request()->isPost()) {
            $where = [];
            // 关键词搜索
            $keywords = $this->request->param('keywords', '');
            if ($keywords) {
                $where[] = ['u.nickname', 'like', '%' . $keywords . '%'];
            }

            $list = PackService::getPackList($where, $this->page, $this->pagesize);
            $count = PackService::countPackList($where);
            return adminResponseJson(['list' => $list, 'count' => $count]);
        }

        // 礼包总数
        $countPack = PackService::countPack();
        // 本月礼包总数
        $countPackWeek = PackService::countPackWeek();
        // 本周礼包总数
        $countPackMonth = PackService::countPackMonth();
        // 社群礼包地区分布
        $packArea = PackService::getPackArea();
        // 社群礼包购买人身份数据
        $identityPackData = PackService::getIdentityPackData();
        return view('', [
            'countPack' => $countPack,
            'countPackWeek' => $countPackWeek,
            'countPackMonth' => $countPackMonth,
            'packArea' => $packArea,
            'identityPackData' => $identityPackData,
        ]);
    }

    /**
     * 社群礼包详情
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function packShow()
    {
        $id = intval($this->request->param('id', 0));
        if (!$id) {
            $this->error(adminJsonMsg(1001));
        }

        $info = PackService::findPack(['p.id' => $id]);
        if (!$info) {
            $this->error(adminJsonMsg(1002));
        }

        return view('', ['info' => $info]);
    }
}